var api = require('../../utils/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // active:0,
    TabCur: 0,
    scrollLeft: 0,
    store_id:'',
    shopInfo:{},
    keywords:'',
    tabList:[],
    cate_id:0,

    option1: [
      { text: '销量', value: 0 },
      { text: '销量升序', value: 1 },
      { text: '销量降序', value: 2 }
    ],
    option2: [
      { text: '价格', value: 'a' },
      { text: '价格升序', value: 'b' },
      { text: '价格降序', value: 'c' }
    ],
    value1: 0,
    value2: 'a',

    array: ['销量', '销量升序', '销量降序'],
    array1: ['价格', '价格升序', '价格降序'],
    idx: 0,
    idx1: 0
  },

  bindPickerChange(e) {
    let idx = e.detail.value
    this.setData({
      idx
    })
    switch (idx) {
      case '0':    //默认销量排序
        this.setData({
          sales: 0
        })
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
      case '1':   //销量升序
        this.setData({
          sales: 2
        })
        console.log(this.data.value1)
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
      case '2':   //销量降序
        this.setData({
          sales: 1
        })
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
    }
  },
  bindPickerChange1(e) {
    let idx1 = e.detail.value
    this.setData({
      idx1
    })
    switch (idx1) {
      case '0':   //价格默认排序
        this.setData({
          price: 0
        })
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
      case '1':   //价格升序
        this.setData({
          price: 2
        })
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
      case '2':   //价格降序
        this.setData({
          price: 1
        })
        this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
        break;
    }
  },

  //上新
  shangXin() {
    this.setData({
      idx: 0,
      idx1: 0
    })

    this.getGoodsList(1, 0, 0, 0);
  },

  //排序
  chooseSort(e) {
    // console.log(e.detail)
    switch (e.detail) {
      case 0:    //默认销量排序
        this.setData({
          sales: 0
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales, this.data.price);
        break;
      case 1:   //销量升序
        this.setData({
          sales: 2
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales, this.data.price);
        break;
      case 2:   //销量降序
        this.setData({
          sales: 1
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales,this.data.price);
        break;
      case 'a':   //价格默认排序
        this.setData({
          price: 0
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales, this.data.price);
        break;
      case 'b':   //价格升序
        this.setData({
          price: 2
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales, this.data.price);
        break;
      case 'c':   //价格降序
        this.setData({
          price: 1
        })
        this.getGoodsList(1,this.data.cate_id,this.data.sales, this.data.price);
        break;
    }
  },
//切换tab
  tabSelect(e) {
    if (e.currentTarget.dataset.idx==this.data.TabCur){return}
    let cate_id = e.currentTarget.dataset.id
    this.setData({
      TabCur: e.currentTarget.dataset.idx,
      scrollLeft: (e.currentTarget.dataset.idx - 1) * 90,
      cate_id
    })
    this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
  },

  productDetail(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?id='+id,
    })
  },


  //搜索
  searchGoods(e) {
    var keywords = e.detail.value
    this.setData({
      keywords
    })

  },
  search1() {
    this.getGoodsList(1, this.data.cate_id, this.data.sales, this.data.price);
  },

  //获取店铺信息
  getShopInfo(){
    api.fetchApi('goods/store_info', { token: wx.getStorageSync('token'), store_id: this.data.store_id}).then(res=>{
      var shopInfo=res.data;
      var tabList = shopInfo.cate;
      this.setData({
        shopInfo,
        tabList
      })
    })
  },
  checktoken(){
    let token = wx.getStorageSync('token')
    if(token==''){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      wx.switchTab({
        url: '/pages/personal/personal',
      })
      return
    }
  },
  //获取商品列表
  getGoodsList(page,cate_id,sales,price){
    var params={
      page,
      token: wx.getStorageSync('token'),
      store_id: this.data.store_id,
      keywords:this.data.keywords,
      sales,
      price,
      cate_id
    }
    api.fetchApi('goods/store_list',params).then(res=>{
      var goodsList = this.data.goodsList;
      var goodsList1 = res.list;
      if (page == 1) {
        goodsList = res.list;
      } else {
        goodsList = [...goodsList, ...goodsList1]
      }
      this.setData({
        goodsList,
        page,
        pageNum: goodsList1.length
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var store_id = options.store_id;
    this.setData({
      store_id
    })
    this.getShopInfo();
    this.getGoodsList(1, 0,0,0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;
    if (this.data.pageNum < 10) {
      wx.showToast({
        title: '暂无更多',
        icon: 'none'
      })
      return
    }
    this.getGoodsList(page, this.data.cate_id, this.data.sales, this.data.price);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})